﻿using System;
using System.IO;
using PolePosition.v3.WcfService.App_Code;

namespace PolePosition.v3.WcfService
{
    public partial class ClearKmlCache : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Returns empty html to browser if query string is wrong
            if (!ValidateQueryStringValues(Request.QueryString["Type"], Request.QueryString["Key"]))
                return;

            //Gets Query String values
            var type = Request.QueryString["Type"];
            var key = Request.QueryString["Key"];
            
            //Fill kmlContent with the cache kml data and response it to the browser
            var kmlMapCacheStrategy = new KmlCacheBuilder(type, key, Path.GetDirectoryName(Server.MapPath(Request.Path)) + @"\KMLCACHE\");
            
            kmlMapCacheStrategy.ClearKmlFile();
        }

        /// <summary>
        /// Validates query string values
        /// </summary>
        /// <param name="type"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public bool ValidateQueryStringValues(string type, string key)
        {
            var validate = true;

            if (string.IsNullOrEmpty(Request.QueryString["Type"]) || string.IsNullOrEmpty(Request.QueryString["Key"]) || Request.QueryString.Keys.Count != 2)
                validate = false;

            return validate;
        }
    }
}
